package ExamSystemServer.server;

import ExamSystemClient.model.ChoiceQuestion;
import ExamSystemClient.model.Student;
import ExamSystemClient.model.User;
import ExamSystemClient.model.UserMessage;

import java.io.EOFException;
import java.io.IOException;
import java.util.List;

/**
 * 实现服务器的主功能
 */
public class ServerView {

    /**
     * 使用合成复用原则
     */
    private ServerInitClose sic;
    private ServerDao sd;
    List<Student> students;
    List<ChoiceQuestion> questions;

    /**
     * 通过构造方法实现成员变量的初始化
     * @param sic
     * @param sd
     */
    public ServerView(ServerInitClose sic, ServerDao sd) {
        this.sic = sic;
        this.sd = sd;
    }

    /**
     * 实现客户端发来消息的接受并处理
     */
    public void serverReceive() throws IOException, ClassNotFoundException {
        //启动时加载学生和考题信息
        students = sd.importStudentInfo();
        questions = sd.importQuestions();
        while(true) {
            String str= (String) sic.getOis().readObject();
            if ("exit".equals(str)){
                //退出时存储学生和考题信息
                sd.serilizeStudentInfo(students);
                sd.serilizeQuestions(questions);
                return;
            }
            if ("manager".equals(str)){
                System.out.println("进入管理员系统。。。");
                if (managerCheck()){
                    //如果登录成功，将学生信息和题库信息发送给服务端
                    sic.getOos().writeObject(students);
                    sic.getOos().writeObject(questions);
                    System.out.println("发送考题和学生信息成功！");
                    //结束操作接受客户端的返回
                    students = (List<Student>) sic.getOis().readObject();
                    questions = (List<ChoiceQuestion>) sic.getOis().readObject();
                }

            }else if ("student".equals(str)){
                System.out.println("进入学生系统。。。");
                int index = studentCheck();
                if(index >= 0){
                    //如果登录成功，将对应学生和题库信息发送给服务端
                    sic.getOos().writeObject(students.get(index));
                    sic.getOos().writeObject(questions);
                    System.out.println("发送考题和学生信息成功！");
                    //结束操作接受客户端的返回,修改students中对应的学生信息
                    students.add(index, (Student) sic.getOis().readObject());
                }
            }
        }
    }

    private boolean managerCheck() throws IOException, ClassNotFoundException {
        UserMessage tum = (UserMessage) sic.getOis().readObject();
        System.out.println("接收到的消息是：" + tum);

        boolean success = false;

        // 调用方法实现管理员账号和密码信息的校验
        if (sd.serverManagerCheck(tum.getUser())) {
            tum.setType("success");
            success = true;
        } else {
            tum.setType("fail");
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器发送校验结果成功！");
        return success;
    }

    private int studentCheck() throws IOException, ClassNotFoundException {
        UserMessage tum = (UserMessage) sic.getOis().readObject();
        System.out.println("接收到的消息是：" + tum);

        int index = -1;
        // 学员账号和密码信息的校验
        boolean success = false;
        for (Student student: students) {
            if (student.getUserName().equals(tum.getUser().getUserName()) &&
                student.getPassword().equals(tum.getUser().getPassword())){
                index = students.indexOf(student);
                break;
            }
        }
        if (index >= 0) {
            tum.setType("success");
        } else {
            tum.setType("fail");
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器发送校验结果成功！");
        return index;
    }
}
